package com.moon.admin.sys.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.moon.admin.framework.MoonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moon.admin.framework.domain.Kv;
import com.moon.admin.framework.domain.MoonPage;
import com.moon.admin.framework.mybatisplus.query.Condition;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.sys.SysConstants;
import com.moon.admin.sys.domain.query.RoleQuery;
import com.moon.admin.sys.entity.SysRoleEntity;
import com.moon.admin.sys.service.SysRoleService;
import com.moon.admin.sys.service.SysUserRoleService;

import io.swagger.annotations.Api;

/**
 * 角色Controller
 *
 * @author moon
 */
@RestController
@RequestMapping("sysRole")
@Api(tags = "角色管理",value = "角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysUserRoleService userRoleService;

    @PostMapping("list")
	public R<MoonPage<SysRoleEntity>> list(@RequestBody RoleQuery query){
		LambdaQueryWrapper<SysRoleEntity> ew = Condition.getQueryWrapper(new SysRoleEntity(), query);
		//排除超级管理员
		ew.ne(SysRoleEntity::getRoleCode, MoonConstants.administration);
		IPage<SysRoleEntity> page = roleService.page(Condition.getPage(query),ew);
		return R.success(MoonPage.of(page));
	}
	
	@SysLog(fun="角色管理",mod="保存角色")
	@PreAuth({"sys_role_add","sys_role_edit"})
	@PostMapping("save")
	@FormRepeat
	public R<Void> save(@RequestBody SysRoleEntity roleEntity){
		roleService.saveOrUpdate(roleEntity);
		return R.success();
	}
	
	@GetMapping(value = "removeById")
	@PreAuth("sys_role_remove")
	@SysLog(fun="角色管理",mod="删除角色")
	@FormRepeat
	public R<Void> removeById(Integer id) {
		if(ToolUtils.isNull(id)) {
			return R.fail("请选择要删除的角色");
		}
		boolean hasUser = userRoleService.hasUser(id);
		if(hasUser) {
			return R.fail("当前角色下已存在用户不能删除");
		}
		return R.status(roleService.removeById(id));
	}
	
	@GetMapping(value = "getMenuPermissionsById")
	@PreAuth("permission_menu")
	public R<Map<String,Object>> getMenuPermissionsById(Integer id){
		Map<String,Object> map = roleService.getMenuPermissionsById(id);
		return R.success(map);
	}
	
	@GetMapping(value = "saveMenuPermission")
	@PreAuth("permission_menu")
	@SysLog(fun="角色管理",mod="功能授权")
	@FormRepeat
	public R<Void> saveMenuPermission(Integer id,String menuIds){
		roleService.saveMenuPermission(id,menuIds);
		return R.success();
	}
	
	/**
	 * 获取所有角色，用于下拉
	 */
	@GetMapping("getList")
	public R<List<Kv>> getList(){
		List<SysRoleEntity> roleList = roleService.list(Wrappers.<SysRoleEntity>lambdaQuery());
		List<Kv> list = roleList.stream().map(entity->Kv.builder().label(entity.getRoleName()).value(String.valueOf(entity.getId())).build()).collect(Collectors.toList());
		return R.success(list);
	}
}